Passed
Pull Request — master (#188)
by Mathieu
02:03
created

GetProjectByIdQueryHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 15 2
1
import {QueryHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {GetProjectByIdQuery} from './GetProjectByIdQuery';
4
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository';
5
import {ProjectView} from '../View/ProjectView';
6
import {ProjectNotFoundException} from 'src/Domain/Project/Exception/ProjectNotFoundException';
7
import {CustomerView} from 'src/Application/Customer/View/CustomerView';
8
9
@QueryHandler(GetProjectByIdQuery)
10
export class GetProjectByIdQueryHandler {
11
  constructor(
12
    @Inject('IProjectRepository')
13
    private readonly projectRepository: IProjectRepository
14
  ) {}
15
16
  public async execute(query: GetProjectByIdQuery): Promise<ProjectView> {
17
    const project = await this.projectRepository.findOneById(query.id);
18
    if (!project) {
19
      throw new ProjectNotFoundException();
20
    }
21
22
    const customer = project.getCustomer();
23
24
    return new ProjectView(
25
      project.getId(),
26
      project.getName(),
27
      project.getDayDuration(),
28
      project.getInvoiceUnit(),
29
      new CustomerView(customer.getId(), customer.getName())
30
    );
31
  }
32
}
33